/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/11/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.manager.workflow.core;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.fireflow.engine.EngineException;
import org.fireflow.engine.IProcessInstance;
import org.fireflow.engine.ITaskInstance;
import org.fireflow.engine.IWorkItem;
import org.fireflow.engine.IWorkflowSession;
import org.fireflow.engine.taskinstance.DefaultTaskInstanceEventListener;

import com.application.usertable.entity.Usertable;
import com.application.usertable.service.UsertableService;
import com.application.yfsmsrecord.entity.YfSmsrecord;
import com.application.yfsmsrecord.service.YfSmsrecordService;
import com.manager.commons.CommonUtil;
import com.manager.workflow.WorkFlowConstants;
import com.workflowdefinition.YfBussTaskinstance;
/**
 * 用该事件监听器替换BasicTaskInstanceManager中的缺省事件监听器。实现“创建工单后给操作员发送短信”
 * @author 王浩 QQ：873987060   
 *
 */
public class BussTaskInstanceEventListener4All extends DefaultTaskInstanceEventListener  {
	 private UsertableService usertableService;
	 private YfSmsrecordService yfSmsrecordService;
	 private WorkFlowCommon workFlowCommon;

	protected void afterWorkItemCreated(IWorkflowSession currentSession,IProcessInstance processInstance,ITaskInstance taskInstance,IWorkItem workItem)throws EngineException{
		if(CommonUtil.SMS_OPEN == 1){
			 Usertable user = new Usertable();
			    user.setId(workItem.getActorId());
			    user = usertableService.getUsertable(user);
	            if(taskInstance.getStepNumber()!=1){  //填写表单的工作项不发送短信
	            	System.out.println("====== WorkItem创建成功，给操作员"+workItem.getActorId()+"短信。。。。。。");
	            	YfBussTaskinstance yfTask = workFlowCommon.getYfBussTaskinstance(taskInstance.getId());
	            	if(yfTask!=null){
	            		sendSms(yfTask,user,workItem);
	            	}
	            }
		}else{
			System.out.println("未开启短信提醒功能！");
		}
		   
//	    	System.out.println("BussTaskInstanceEventListener4All:: WorkItem is "+workItem);
//	    	System.out.println("BussTaskInstanceEventListener4All:: currentSession is "+currentSession);
//	    	System.out.println("BussTaskInstanceEventListener4All:: processInstance is "+processInstance);
//	    	System.out.println("BussTaskInstanceEventListener4All:: taskInstance is "+taskInstance);
	}
	 
	
    private void sendSms(YfBussTaskinstance yfTask,Usertable user,IWorkItem workItem){
    	String bussName = "";
    	YfSmsrecord smsrecord = new YfSmsrecord();
    	smsrecord.setBussId(yfTask.getBussId());
    	if(yfTask.getBussType()!=null){
    		bussName = CommonUtil.getDictValue(yfTask.getBussType().intValue(), "BUSS_TYPE");
    	}
    	if(StringUtils.isNotBlank(bussName)){
    		bussName = "("+bussName+")";
    	}
    	
    	smsrecord.setBussType(yfTask.getBussType());
    	smsrecord.setOrg(yfTask.getProjectOrg());
    	smsrecord.setReceNo(user.getTel());
    	smsrecord.setReceUser(user.getRname());
    	smsrecord.setSendContent("您有新的"+bussName+"代办项，请及时处理！");
    	smsrecord.setSendTime(new Date());
    	smsrecord.setWorkitmeId(workItem.getId());
    	smsrecord.setCreateDate(new Date());
    	smsrecord.setOperateid("-1");
    	smsrecord.setSendType(new Long(0));
    	smsrecord.setState(WorkFlowConstants.SMS_STATUS_1);
    	yfSmsrecordService.insertObject(smsrecord);
    	System.out.println("发送短信到" + smsrecord.getReceNo() + "  " + smsrecord.getReceUser());
    	CommonUtil.smsSend.seneSms(smsrecord);
    }
	 


	public YfSmsrecordService getYfSmsrecordService() {
		return yfSmsrecordService;
	}



	public void setYfSmsrecordService(YfSmsrecordService yfSmsrecordService) {
		this.yfSmsrecordService = yfSmsrecordService;
	}

	public WorkFlowCommon getWorkFlowCommon() {
			return workFlowCommon;
	}

	public void setWorkFlowCommon(WorkFlowCommon workFlowCommon) {
		this.workFlowCommon = workFlowCommon;
	}
	public UsertableService getUsertableService() {
		return usertableService;
	}






	public void setUsertableService(UsertableService usertableService) {
		this.usertableService = usertableService;
	}
}
